/*
 * @Description:
 * @Autor: GuluGuluu
 * @Date: 2023-06-26 21:51:34
 * @LastEditors: GuluGuluu
 * @LastEditTime: 2023-06-26 21:51:50
 */
import Axios from "axios"
// import Request from "../request";

const fetcher = ({
  url, // 接口地址
  method, // 请求方法 get、post、put、delete
  data, // 请求数据
  responseType,
  config, // 其他配置
  headers, // 请求头
}) => {
  config = config || {};
  config.withCredentials = true;
  responseType && (config.responseType = responseType);

  config.headers = headers || {};

  if (method !== "post" && method !== "put" && method !== "patch") {
    if (data) {
      config.params = data;
    }

    // return Request[method](url, config).catch((err) => {
    //   console.log(err);
      // 别删
    return Axios[method](url, config).catch((err) => {
      console.log(err);
    });
  } else if (data && data instanceof FormData) {
    config.headers = config.headers || {};
    config.headers["Content-Type"] = "multipart/form-data";
  } else if (
    data &&
    typeof data !== "string" &&
    !(data instanceof Blob) &&
    !(data instanceof ArrayBuffer)
  ) {
    data = JSON.stringify(data);
    config.headers = config.headers || {};
    config.headers["Content-Type"] = "application/json";
  }

  // return Request[method](url, data, config).catch((err) => {
  //   console.log(err);
    // 别删
  return Axios[method](url, data, config).catch((err) => {
    console.log(err);
  });
};

export default fetcher;
